package com.gameloft.input;


public class Pointer
{
  
  private final static int STATE_NONE = 0;
  private final static int STATE_JUST_PRESSED = 1;
  private final static int STATE_PRESSED = 2;
  private final static int STATE_DRAGGING = 3;
  private final static int STATE_JUST_RELEASED = 4;
  private final static int STATE_RELEASED = 5;
  
  private static int state = STATE_NONE;
  private static int pressX = -1;
  private static int pressY = -1;
  private static int currentX = -1;
  private static int currentY = -1;
  private static int releaseX = -1;
  private static int releaseY = -1;
  
  
  public static boolean isPressed()
  {
  	return (state == STATE_PRESSED);
  }
	
  public static boolean isDragging()
  {
  	return (state == STATE_DRAGGING);
  }
	
  public static boolean isReleased()
  {
		return (state == STATE_RELEASED);
  }
  
  public static boolean isInRect(int x, int y, int w, int h)
  {
  	return ((currentX >= x) && (currentX < x+w) && (currentY >= y) && (currentY < y+h));
  }
	
  public static boolean pressedInRect(int x, int y, int w, int h)
  {
  	return ((pressX >= x) && (pressX < x+w) && (pressY >= y) && (pressY < y+h));
  }
	
  public static boolean releasedInRect(int x, int y, int w, int h)
  {
  	return ((releaseX >= x) && (releaseX < x+w) && (releaseY >= y) && (releaseY < y+h));
  }
	
  public static boolean clickedInRect(int x, int y, int w, int h)
  {
  	return (pressedInRect(x, y, w, h) && releasedInRect(x, y, w, h));
  }
  
  public static void pointerPressed(int x, int y)
  {
		pressX = x;
		pressY = y;
		currentX = x;
		currentY = y;
		state = STATE_JUST_PRESSED;
  }
  
  public static void pointerDragged(int x, int y)
  {
		currentX = x;
		currentY = y;
  }
  
  public static void pointerReleased(int x, int y)
  {
		releaseX = x;
		releaseY = y;
		state = STATE_JUST_RELEASED;
  }
  
  public static void update()
  {
  	switch (state)
  	{
  		case STATE_JUST_PRESSED: state = STATE_PRESSED; break;
  		case STATE_PRESSED: state = STATE_DRAGGING; break;
  		case STATE_JUST_RELEASED: state = STATE_RELEASED; break;
  		case STATE_RELEASED: state = STATE_NONE; break;
  	}
  	
		if (state == STATE_NONE)
  	{
  		pressX = -1;
  		pressY = -1;
			currentX = -1;
			currentY = -1;
			releaseX = -1;
			releaseY = -1;
		}
  }
  
}